package com.muapet.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.muapet.mapper.OrderMapper;
import com.muapet.model.Account;
import com.muapet.model.Order;
import com.muapet.model.Pet;
import com.muapet.model.User;
import com.muapet.model.other.AddressInfo;
import com.muapet.model.other.Pagination;
import com.muapet.model.other.sendInfo;
import com.muapet.service.IOrderService;
import com.muapet.service.IPetService;
import com.muapet.service.IUserService;

@Service
public class OrderServiceImpl implements IOrderService {

	private IPetService petService;
	private IUserService userService;

	private OrderMapper orderMapper;

	@Autowired
	public void setPetService(IPetService petService) {
		this.petService = petService;
	}

	@Autowired
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Autowired
	public void setOrderMapper(OrderMapper orderMapper) {
		this.orderMapper = orderMapper;
	}

	@SuppressWarnings("unused")
	@Override
	public Integer addOrder(Integer petId, Integer buyerId,
			AddressInfo addressInfo) {
		Pet pet = petService.findById(petId);
		User seller = userService.findById(pet.getSellerId());
		User buyer = userService.findById(buyerId);
		Account account = userService.findAccountByUserId(buyerId);
		if (pet == null) {
			return -2;
		} else if (pet.getBuyerId() != null && pet.getBuyerId() != 0) {
			return -1;
		}
		if (account == null || account.getBalance() < pet.getPrice()) {
			return -3;
		}
		if (seller.getId().equals(buyerId)) {
			return -4;
		}
		Order order = new Order();
		Random rd = new Random();
		Calendar time = Calendar.getInstance();
		String orderNo = String.valueOf(time.get(Calendar.YEAR))
				+ String.valueOf(time.get(Calendar.MONTH) + 1)
				+ String.valueOf(time.get(Calendar.DAY_OF_MONTH))
				+ String.valueOf(time.get(Calendar.HOUR_OF_DAY))
				+ String.valueOf(time.get(Calendar.MINUTE))
				+ String.valueOf(time.get(Calendar.SECOND))
				+ String.valueOf(rd.nextInt(100));
		order.setOrderNo(orderNo);
		order.setPetId(petId);
		order.setPetTitle(pet.getTitle());
		order.setSellerId(seller.getId());
		order.setSellerName(seller.getName());
		order.setBuyerId(buyer.getId());
		order.setBuyerName(buyer.getName());
		order.setPrice(pet.getPrice());
		order.setBuyerConsignee(addressInfo.getConsignee());
		order.setBuyerTel(addressInfo.getTel());
		order.setBuyerAddress(addressInfo.getAddress());
		order.setState(Order.ORDER_STATE_NEW);
		order.setCreateDate(new Date());
		orderMapper.insertSelective(order);

		pet.setBuyerId(buyerId);
		petService.update(pet);

		return order.getId();
	}

	@Override
	public Order findById(Integer id) {
		return orderMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Order> listOrdersByUserId(Integer userId) {
		Example orderExample = new Example(Order.class);
		orderExample.or().andEqualTo("sellerId", userId);
		orderExample.or().andEqualTo("buyerId", userId);
		orderExample.orderBy("createDate").desc();
		return orderMapper.selectByExample(orderExample);
	}

	@Override
	public List<Order> listUndoOrdersByUserId(Integer userId) {
		Example orderExample = new Example(Order.class);
		orderExample.and().andNotEqualTo("state", Order.ORDER_STATE_DONE);
		orderExample.and().orEqualTo("sellerId", userId)
				.orEqualTo("buyerId", userId);
		return orderMapper.selectByExample(orderExample);
	}

	@Override
	public Integer pay(Integer id) {
		Order order = new Order();
		order.setId(id);
		order.setState(Order.ORDER_STATE_TOSEND);
		return orderMapper.updateByPrimaryKeySelective(order);
	}

	@Override
	public Integer send(Integer id, sendInfo sendInfo) {
		Order order = new Order();
		order.setId(id);
		if (sendInfo != null) {
			order.setExperssNo(sendInfo.getExperssNo());
		}
		order.setState(Order.ORDER_STATE_TORECEIVE);
		return orderMapper.updateByPrimaryKeySelective(order);
	}

	@Override
	public Integer receive(Integer id) {
		Order order = new Order();
		order.setId(id);
		order.setDealDate(new Date());
		order.setState(Order.ORDER_STATE_DONE);
		return orderMapper.updateByPrimaryKeySelective(order);
	}

	@Override
	public List<Order> findAll(Pagination pagination) {
		return orderMapper.selectByRowBounds(null,
				new RowBounds(pagination.getOffset(), pagination.getLimit()));
	}

	@Override
	public Integer countAll() {
		return orderMapper.selectCount(null);
	}

	@Override
	public List<Order> findByOrderNo(String orderNo) {
		Order order = new Order();
		order.setOrderNo(orderNo);
		return orderMapper.select(order);
	}

	@Override
	public Integer delete(Integer id) {
		return orderMapper.deleteByPrimaryKey(id);
	}
}
